package cn.timeapi.Applet.Mall.CatSquare.controller.portal;

import cn.timeapi.Applet.Mall.CatSquare.common.*;
import cn.timeapi.Applet.Mall.CatSquare.entity.Shipping;
import cn.timeapi.Applet.Mall.CatSquare.entity.Account;
import cn.timeapi.Applet.Mall.CatSquare.service.IShippingService;
import cn.timeapi.Applet.Mall.CatSquare.entity.Account;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

/**
 * 描述:收货地址的功能模块
 * @author Vincent Vic
 * create 2020-02-21 13:18
 */
@Api("shipping")
@Controller
@RequestMapping("/shipping/")   //请求地址设置
public class ShippingController {

    @Autowired
    IShippingService iShippingService;
    
    @Autowired
    private RedisOperator<String,Account> redis;

    // 增
    // 通过类，作为对象绑定接口
    @ApiOperation("添加地址")
    @GetMapping("add")
    @ResponseBody   //自动序列化Json
    public ServerResponse add(@ApiParam("会话码") @ParamsNotNull String sessionKey, @ApiParam("地址属性") Shipping shipping){
        
        //判断是否登入
		Account account = redis.get(Const.REDIS_LOGIN_PREFIX + sessionKey);
        if (account == null){
            return ServerResponse.error(ResponseCode.NEED_LOGIN.getCode(),"未登录，需要登录");
        }

        return iShippingService.add(account.getOpenId(),shipping);
    }

    // 删
    @ApiOperation("删除地址")
    @GetMapping("del")
    @ResponseBody   //自动序列化Json
    public ServerResponse deleteShipping(@ApiParam("会话码") @ParamsNotNull String sessionKey, @ApiParam("地址id") @ParamsNotNull Integer shippingId){
        //判断是否登入
		Account account = redis.get(Const.REDIS_LOGIN_PREFIX + sessionKey);
        if (account == null){
            return ServerResponse.error(ResponseCode.NEED_LOGIN.getCode(),"未登录，需要登录");
        }

        return iShippingService.deleteShipping(account.getOpenId(),shippingId);
    }

    // 改
    @ApiOperation("修改地址")
    @GetMapping("update")
    @ResponseBody   //自动序列化Json
    public ServerResponse update(@ApiParam("会话码") @ParamsNotNull String sessionKey, @ApiParam("地址属性") Shipping shipping){
        
        //判断是否登入
		Account account = redis.get(Const.REDIS_LOGIN_PREFIX + sessionKey);
        if (account == null){
            return ServerResponse.error(ResponseCode.NEED_LOGIN.getCode(),"未登录，需要登录");
        }

        return iShippingService.update(account.getOpenId(),shipping);
    }

    // 查
    @ApiOperation("查询地址详情")
    @GetMapping("select")
    @ResponseBody   //自动序列化Json
    public ServerResponse<Shipping> select(@ApiParam("会话码") @ParamsNotNull String sessionKey, @ApiParam("地址id") @ParamsNotNull Integer shippingId){
        
        //判断是否登入
		Account account = redis.get(Const.REDIS_LOGIN_PREFIX + sessionKey);
        if (account == null){
            return ServerResponse.error(ResponseCode.NEED_LOGIN.getCode(),"未登录，需要登录");
        }

        return iShippingService.select(account.getOpenId(),shippingId);
    }

    // 查
    @ApiOperation("查询地址列表")
    @GetMapping("list")
    @ResponseBody   //自动序列化Json
    public ServerResponse<PageInfo> list(@ApiParam("会话码") @ParamsNotNull String sessionKey, @ApiParam("分页起始 默认：1") @RequestParam(value = "pageNum" ,defaultValue = "1") int pageNum,@ApiParam("分页结束 默认10") @RequestParam(value = "pageSize" ,defaultValue = "10") int pageSize){
        
        //判断是否登入
		Account account = redis.get(Const.REDIS_LOGIN_PREFIX + sessionKey);
        if (account == null){
            return ServerResponse.error(ResponseCode.NEED_LOGIN.getCode(),"未登录，需要登录");
        }

        return iShippingService.list(account.getOpenId(),pageNum,pageSize);
    }


}